package com.xiaogang.ggfw.config.swagger;

import io.swagger.models.auth.In;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName : caldav
 * @作者 : 侯小刚
 * @描述 : swagger配置
 * @创建日期 : 2023/4/10 10:48
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    @Bean("clientDocket")
    public Docket clientDocket() {
        return createDocker("A【v1】服务API", "/api/v1/service/**");
    }

    @Bean("backendDocket")
    public Docket backendDocket() {
        return createDocker("B【v1】系统后台API", "/api/v1/backend/**");
    }

    /**
     * 全局参数配置
     * @return
     */
    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", In.HEADER.toValue()));
        return apiKeyList;
    }

    /**
     * 安全上下文
     * @return
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    /**
     * 创建Docket
     * @param groupName
     * @param api
     * @return
     */
    public Docket createDocker(String groupName, String api) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("caldav")
                        .description("caldav接口文档")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName(groupName)
                // 是否启用Swagger
                .enable(true)
                .select()
                .paths(PathSelectors.ant(api)).build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }



}
